package com.example.ajaxdemo.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName Demo2Controller
 * @Description
 * @Author zhangsw
 * @Date 2020/11/6 14:53
 * @Version 1.0
 */
@RestController
public class Demo2Controller {

    @GetMapping("demo1")
    public ModelAndView demo1() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("JqAjax");
        return mv;
    }

    @GetMapping("demo2")
    public ModelAndView demo2() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Axios_Ajax");
        return mv;
    }

    @GetMapping("demo3")
    public ModelAndView demo3() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("fetch-demo");
        return mv;
    }

    @PutMapping("put1")
    public String put1() {
        return "put1";
    }

    @DeleteMapping("delete1")
    public String delete1() {
        return "delete请求来的";
    }

    @GetMapping("axios-demo1")
    public String axios_demo1(HttpServletRequest req,String id,String vip) {
        String name = req.getHeader("name");
        String age = req.getHeader("age");
        return name + ":" + age + "&" + id + ":" + vip;
    }

    @PostMapping("axios-demo2")
    public String axios_demo2(HttpServletRequest req,String id,String vip, String username, String password) {
        String height = req.getHeader("height");
        String weight = req.getHeader("weight");

        return height + weight + id + vip + username + password;
    }

    @PostMapping("fetch_post")
    public String fetch_post(String vip,String username,String password, HttpServletRequest req) {
        String name = req.getHeader("name");
        return vip + username + password + name;
    }

    @GetMapping("toFlushTest")
    public ModelAndView toFlushTest(ModelAndView mv) {
        mv.setViewName("flushTest");
        return mv;
    }

    @GetMapping("demodemo")
    public ModelAndView demodemo(ModelAndView mv) {
        mv.setViewName("demo");
        return mv;
    }

    @GetMapping("getResult/{id}")
    @ResponseBody
    public String getResult(@PathVariable("id")Integer id,HttpServletRequest request) {
        String type = request.getParameter("type");
        return "hello id：" + id + " type：" + type;
    }

    public static void main(String[] args) {
        File file = new File("D:\\qmt\\demo.html");
        if (file.exists()) {
            file.delete();
            System.out.println("删除成功");
        } else {
            System.out.println("文件不存在");
        }
    }
}
